package com.uzhie.jt808.jt1078.entity;

import com.uzhie.jt808.JT808MsgBody;
import com.uzhie.jt808.msg.entity.BitConverter;
import com.uzhie.jt808.msg.entity.MyBuffer;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * 1078 流媒体参数设置
 * Created by lilongfei on 2018/8/9.
 */
public class JT_8103 implements JT808MsgBody {

    /**
     * 参数总数
     */
    byte count;
    /**
     * 参数项列表
     */
    List<JT_8103_Catalog> list;

    public byte getCount() {
        return count;
    }

    public void setCount(byte count) {
        this.count = count;
    }

    public List<JT_8103_Catalog> getList() {
        return list;
    }

    public void setList(List<JT_8103_Catalog> list) {
        this.list = list;
    }

    public JT_8103(List<JT_8103_Catalog> catalogs){
        this.count = BitConverter.GetBytes(catalogs.size())[3];
        this.list = catalogs;
    }

    @Override
    public byte[] WriteToBytes() {
        MyBuffer buffer = new MyBuffer();
        buffer.put(count);
        for (JT_8103_Catalog catalog :list){
            buffer.put(catalog.WriteToBytes());
        }
        return buffer.array();
    }

    @Override
    public void ReadFromBytes(byte[] body) {
        MyBuffer buffer = new MyBuffer(body);
        setCount(buffer.get());
        List<JT_8103_Catalog> catalogs = new ArrayList<>();
        while (buffer.hasRemain()) {
            JT_8103_Catalog catalog = new JT_8103_Catalog();
            int length = BitConverter.ToUInt32(buffer.get());
            catalog.setID(buffer.gets(4));
            catalog.setLength(length);
            catalog.setParam(buffer.gets(length));
            catalogs.add(catalog);
        }
    }

/*    @Override
    public String toString() {
        StringBuilder sBuilder = new StringBuilder();
        sBuilder.append(String.format(
                "传递的命令总数：%1$s", getCount()));
        return sBuilder.toString();
    }*/
}
